package com.woniuxy.yogaapp.handler;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.woniuxy.yogaapp.pojo.Chat;
import com.woniuxy.yogaapp.pojo.Coach;
import com.woniuxy.yogaapp.pojo.Student;
import com.woniuxy.yogaapp.pojo.YogaClub;
import com.woniuxy.yogaapp.service.YogaClubService;
import com.woniuxy.yogaapp.util.RegexUtil;

import lombok.Data;

@Data
@Controller
@RequestMapping("/yogaclub")
@Scope("prototype")
public class YogaClubHandler {
	@Resource
	private YogaClubService yogaClubService;

	/**
	 * 场馆处理 教练申请加入信息 同意请求 需要参数 sender(发送人、uid外键int) recipient（接收人、uid外键int）
	 * 
	 * @return
	 */
	@RequestMapping("/agree")
	@ResponseBody
	public String agreeApplyFor(Chat chat) {
		String result=yogaClubService.agreeApplyFor(chat);
		return result;

	}

	/**
	 * 场馆处理 教练申请加入信息 拒绝请求 需要参数 sender(发送人、uid外键int) recipient（接收人、uid外键int）
	 * 
	 * @return
	 */
	@RequestMapping("/refuse")
	@ResponseBody
	public String refuseApplyFor(Chat chat) {
		String result=yogaClubService.refuseApplyFor(chat);
		return result;

	}

	// 查询所有
	@RequestMapping("/all")
	@ResponseBody
	public List<YogaClub> yogaClubAll() {
		return yogaClubService.findAllYogaClub();
	}

	// 根据会馆id查询相对应的教练信息再查询出学员信息
	@RequestMapping("/byyid")
	@ResponseBody
	public List<Coach> ClubByYidCoach() {

		YogaClub y = yogaClubService.findByYid(1);
		System.out.println(y.getCoachs());
		return y.getCoachs();
	}

	// 根据会馆id查询相对应的教练信息
	@RequestMapping("/bycoach")
	@ResponseBody
	public List<Coach> ClubByCoach() {
		YogaClub yc = yogaClubService.findByCoach(1);
		System.out.println(yc.getCoachs());
		return yc.getCoachs();
	}
	 /*
     * 点击个人信息完善 查看数据库查看该用户的个人信息 显示到前端
     */
	@RequiresRoles(value = { "yogaclub", "common" }, logical = Logical.OR)
    @RequestMapping("/showMe")
    public ModelAndView showMe() {
       YogaClub yogaClub=new YogaClub();
        // 1.获取subject对象
        Subject currentUser = SecurityUtils.getSubject();
        Session session = currentUser.getSession();
        int uid = (int) session.getAttribute("uid");
        yogaClub.setUid(uid);
        YogaClub  yogaClubMe=  yogaClubService.findYogaClubByUid(yogaClub);
        if(yogaClubMe==null){
        	yogaClubMe=new YogaClub();
        }
        // 3.跳转页面
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("yogaclub", yogaClubMe);
        modelAndView.setViewName("/yogaClubInfo.html");
        return modelAndView;
    }

	/**
	 * 添加会馆基本信息
	 */
	@RequestMapping("/info")
	public String addYogaClubInfo(YogaClub yogaClub,HttpServletRequest request) {
		String result = "失败！";
		// 1.获取subject对象
		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();
		int uid = (int) session.getAttribute("uid");
		yogaClub.setUid(uid);
		// 1.会馆名字不能为空
		if (yogaClub.getYname() == null || yogaClub.getYname().length() == 0) {
			request.setAttribute("yogaclubtishi", "会馆名字不能为空！");
			return "/html/yogaclubtishi.html";
		}
		// 2.会馆地址不能为空
		if (yogaClub.getYaddress() == null || yogaClub.getYaddress().length() == 0) {
			request.setAttribute("yogaclubtishi", "会馆地址不能为空！");
			return "/html/yogaclubtishi.html";
		}
		// 3.电话不能为空
		if (yogaClub.getYtel() == null || yogaClub.getYtel().length() == 0) {
			request.setAttribute("yogaclubtishi", "电话号码不能为空！");
			return "/html/yogaclubtishi.html";
		}
		// 电话合法性判断
		if (!RegexUtil.isMobile(yogaClub.getYtel())) {
			request.setAttribute("yogaclubtishi", "电话号码格式不正确！");
			return "/html/yogaclubtishi.html";
		}
		// 先在数据库查询有无该企业基本信息，如果有则更改该信息 如果没有则新增
		YogaClub yoga = yogaClubService.findYogaClubByUid(yogaClub);
		if (yoga == null) {
			// 新增
			result = yogaClubService.addYogaClubInfo(yogaClub);
		} else {
			// 更改信息
			result = yogaClubService.updateYogaClubInfo(yogaClub);
		}
		request.setAttribute("yogaclubtishi", result);
		return "/html/yogaclubtishi.html";
	}

	/**
	 * 会馆认证请求 需要前端参数 1.负责人：legalperson 2.负责人身份证：yidcard
	 */
	@RequestMapping("/approve")
	@ResponseBody
	public String approvePower(YogaClub yogaClub) {
		String result = "发送认证请求失败！";
		// 1.获取subject对象
		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();
		int uid = (int) session.getAttribute("uid");
		yogaClub.setUid(uid);
		// 判断该是否完成场馆基本信息填写
		YogaClub yoga = yogaClubService.findYogaClubByUid(yogaClub);
		if (yoga == null) {
			return "你还未进行基本信息填写不能进行此操作！";
		}
		// 填写 认证信息 给管理员发送通知 与短信
		try {
			result = yogaClubService.approvePower(yogaClub);
		} catch (Exception e) {

		}

		return result;

	}

}
